/*
 * =====================================================================================
 *
 *       Filename:  perm1.cc
 *
 *    Description:  simplest permutation
 *
 *        Version:  1.0
 *        Created:  12-04-19 02:16:39 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

/*  
 *  Assume we have a group of numbers or chars p = {r1, r2, r3,..., rn} and the full perm is
 *	perm(p), pn=p - {rn}.
 *  
 *  The algorithm is perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), ... , rnperm(pn).
 *  
 *  The programming below means: exchange other number with the first number and then process
 *  the latter n-1 numbers.
 *
 *  However, I still don't really understand why the second swap is required.... it should mean
 *  swap back
 *
 *  */



#include <stdlib.h>

#include <iostream>
using namespace std;

void Perm1(char* pStr, char* pBegin){
	if(*pBegin == '\0')
		cout<<"output: "<<pStr<<endl;
	else {
		for(char* pCh=pBegin; *pCh!='\0'; pCh++){
			
			cout<<pBegin<<" ";
			swap(*pBegin, *pCh);
			cout<<"swap once: "<<*pBegin<<"<->"<<*pCh<<" "<<pBegin<<"("<<pStr<<")"<<" ";
			Perm1(pStr, pBegin+1);
			swap(*pBegin, *pCh);
			cout<<"swp twice: "<<*pBegin<<"<->"<<*pCh<<" "<<pBegin<<"("<<pStr<<")"<<endl;
		}
	}
}


int main(void){
	char str[] = "abcd";
	Perm1(str, str);
	return 0;
}
